package org.example.workbench.service.impl;

import org.example.commons.constant.Constant;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.User;
import org.example.workbench.domain.Customer;
import org.example.workbench.domain.Funnel;
import org.example.workbench.domain.Tran;
import org.example.workbench.domain.TranHistory;
import org.example.workbench.mapper.CustomerMapper;
import org.example.workbench.mapper.TranHistoryMapper;
import org.example.workbench.mapper.TranMapper;
import org.example.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service("tranService")
public class tranImpl implements TranService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private TranMapper tranMapper;
    @Autowired
    private TranHistoryMapper tranHistoryMapper;

    @Override
    public int getTotalCountTran(Map<String, Object> map) {
        return tranMapper.getTotalCountTran(map);
    }

    @Override
    public List<Tran> getTranPagesByMap(Map<String, Object> map) {
        return tranMapper.getTranPagesByMap(map);
    }

    @Override
    public List<Tran> getTranByContactsId(String contactsId) {
        return tranMapper.getTranByContactsId(contactsId);
    }

    @Override
    public int deleteTranById(String tranId) {
        return tranMapper.deleteTranById(tranId);
    }

    @Override
    public List<Tran> getTranDetailByCustomerId(String customerId) {
        return tranMapper.getTranDetailByCustomerId(customerId);
    }

    @Override
    public List<Funnel> getStageAndStageNumber() {
        return tranMapper.getStageAndStageNumber();
    }

    @Override
    public Tran getTranDetailByid(String id) {
        return tranMapper.getTranDetailByid(id);
    }

    @Override
    public void saveTran(Map<String, Object> map) {
        User user =(User) map.get(Constant.SESSION_USER);
        Customer customer = customerMapper.getCustomerDetailByName((String) map.get("accountName"));
        if (customer==null){
            customer = new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(user.getId());
            customer.setName((String) map.get("accountName"));
            customer.setCreateBy(user.getId());
            customer.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
            customerMapper.insertCustomerByCustomer(customer);
        }
        //插入交易信息
        Tran tran = new Tran();
        tran.setId(UUIDUtils.getUUID());
        tran.setOwner((String) map.get("owner"));
        tran.setMoney((String) map.get("money"));
        tran.setName((String) map.get("name"));
        tran.setExpectedDate((String) map.get("expectedDate"));
        tran.setCustomerId(customer.getId());
        tran.setStage((String) map.get("stage"));
        tran.setSource((String) map.get("source"));
        tran.setActivityId((String) map.get("activityId"));
        tran.setContactsId((String) map.get("contactsId"));
        tran.setType((String) map.get("type"));
        tran.setCreateBy(user.getId());
        tran.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        tran.setDescription((String) map.get("description"));
        tran.setContactSummary((String) map.get("contactSummary"));
        tran.setNextContactTime((String) map.get("nextContactTime"));
        tranMapper.insertOneByTran(tran);
        //插入交易历史信息
        TranHistory tranHistory = new TranHistory();
        tranHistory.setId(UUIDUtils.getUUID());
        tranHistory.setStage(tran.getStage());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setCreateBy(tran.getCreateBy());
        tranHistory.setCreateTime(tran.getCreateTime());
        tranHistory.setTranId(tran.getId());
        tranHistoryMapper.insertTranHistoryByTranHistory(tranHistory);
    }
}
